package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/gojsonq/v2"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
)

func AddBMUCollect(c *gin.Context) {
	body, _ := ioutil.ReadAll(c.Request.Body)
	bmuId := gojsonq.New().FromString(string(body)).Find("bmuId")
	cells := gojsonq.New().FromString(string(body)).Find("cell")
	temperatures := gojsonq.New().FromString(string(body)).Find("temp")
	smokes := gojsonq.New().FromString(string(body)).Find("smoke")
	currents := gojsonq.New().FromString(string(body)).Find("curr")
	valid := validation.Validation{}
	valid.Required(bmuId, "bmuId").Message("bmuId不能为空")
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.ERROR_NOT_EXIST
		tmpBMUId, _ := strconv.Atoi(bmuId.(string))
		if models.ExistBMUById(tmpBMUId) {
			code = e.SUCCESS
			if cells != nil {
				for _, cell := range cells.([]interface{}) {
					tmpPointId , _:= strconv.Atoi(cell.(map[string]interface{})["id"].(string))
					tmpVoltage := cell.(map[string]interface{})["vol"].(float64)
					tmpSoc := cell.(map[string]interface{})["soc"].(float64)
					models.AddBMUBattery(tmpPointId, tmpBMUId, tmpVoltage, tmpSoc)
				}
			}

			if temperatures != nil {
				for _, temp := range temperatures.([]interface{}) {
					tmpPointId, _ := strconv.Atoi(temp.(map[string]interface{})["id"].(string))
					tmpTemperature := temp.(map[string]interface{})["val"].(float64)
					models.AddBMUTemperature(tmpPointId, tmpBMUId, tmpTemperature)
				}
			}

			if smokes != nil {
				for _, smoke := range smokes.([]interface{}) {
					tmpPointId, _ := strconv.Atoi(smoke.(map[string]interface{})["id"].(string))
					tmpSmoke := smoke.(map[string]interface{})["val"].(float64)
					models.AddBMUSmoke(tmpPointId, tmpBMUId, tmpSmoke)
				}
			}

			if currents != nil {
				for _, curr := range currents.([]interface{}) {
					tmpPointId, _ := strconv.Atoi(curr.(map[string]interface{})["id"].(string))
					tmpCurrent := curr.(map[string]interface{})["val"].(float64)
					models.AddBMUCurrent(tmpPointId, tmpBMUId, tmpCurrent)
				}
			}
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func GetBMUOutline(c *gin.Context) {
	data := make(map[string]interface{})
	bmuId := c.Query("bmuId")
	valid1 := validation.Validation{}
	valid1.Required(bmuId, "bmuId").Message("bmuId不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.ERROR_NOT_EXIST
		tmpBMUId, _ := strconv.Atoi(bmuId)
		if models.ExistBMUById(tmpBMUId) {
			code = e.SUCCESS
			data = models.GetBMUOutline(tmpBMUId)
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetBMUCollects(c *gin.Context) {
	var data []models.BMUCollect
	bmuId := c.Query("bmuId")
	duration := c.Query("duration")
	valid1 := validation.Validation{}
	valid1.Required(bmuId, "bmuId").Message("bmuId不能为空")
	valid1.Required(duration, "duration").Message("duration不能为空")
	code := e.INVALID_PARAMS
	if !valid1.HasErrors() {
		code = e.ERROR_NOT_EXIST
		tmpBMUId, _ := strconv.Atoi(bmuId)
		tmpDuration, _ := strconv.Atoi(duration)
		if models.ExistBMUById(tmpBMUId) {
			code = e.SUCCESS
			data = models.GetBMUCollects(tmpBMUId, tmpDuration)
		}
	} else {
		for _, err := range valid1.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}